package com.iniyo.monitor.client.service;

import okhttp3.*;

import java.io.IOException;

public class HttpServiceImpl implements HttpService {
    OkHttpClient okHttpClient;

    public HttpServiceImpl() {
        okHttpClient = new OkHttpClient.Builder()
                .build();
        okHttpClient.dispatcher().setMaxRequestsPerHost(100000);
    }

    public void concurrentHttp(String url, int threadNumber) {
        for (int i = 0; i < threadNumber; i++) {
            new HttpThread(url).start();
        }
    }

    private class HttpThread extends Thread {

        private String url;

        public HttpThread(String url) {
            this.url = url;
        }

        @Override
        public void run() {
            final Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            try {
                Response resp = okHttpClient.newCall(request).execute();
                System.out.println(resp.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
